package net.jsoft;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReadTxt
{
    public static void main(String[] args)
    {
        ReadTxt rt = new ReadTxt();
        rt.readTxt();
    }

    public void readTxt()
    {
        String filePath = "C:\\Users\\JOHN_ZHAO\\Desktop\\lulu.txt";
        InputStreamReader is1 = null;
        BufferedReader bufferedReader = null;
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        try
        {
            String encoding = "UTF-8";
            File file = new File(filePath);
            is1 = new InputStreamReader(new FileInputStream(file), encoding);//考虑到编码格式

            bufferedReader = new BufferedReader(is1);
            String lineTxt = null;
            Map<String, String> map = null;
            while ((lineTxt = bufferedReader.readLine()) != null)
            {
                //在这里封装你想要的
                System.out.println(lineTxt);
                //每行的内容 ，并且用|分隔 ，拆分后变成数组
                String[] arr = lineTxt.split("\\|", -1);
                map = new HashMap<String, String>();
                map.put("商户号", arr[0]);
                map.put("终端号", arr[1]);
                map.put("扣款银行卡号", arr[2]);
                map.put("缴费金额", arr[3]);
                map.put("社保交易流水号", arr[4]);
                map.put("银联检索参考号", arr[5]);
                list.add(map);
            }

            //这样文件里所有的内容都在list里了

            int i = 1;
            for (Map<String, String> m : list)
            {
                for (String k : m.keySet())
                {
                    System.out.println("第" + i + "行" + k + ":" + m.get(k));
                }
                i++;

            }

        }
        catch (Exception e)
        {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        finally
        {
            if (is1 != null)
            {
                try
                {
                    is1.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

    }

}
